/*
 * Gedigarr - Falling block game.
 * Copyright (C) 2010  BoredCorp
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package gedigarr.objects;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.Rectangle2D;

/**
 *
 * @author JC
 */
public class CharacterCell extends Cell {

  private Font font = new Font( "Courier New", Font.BOLD, 20 );
  private Rectangle2D charBounds;
  private GlyphVector glyph = null;
  Color fgColour = Color.WHITE;
  char character = ' ';
  double charX = 0;
  double charY = 0;
  protected boolean recalcBounds = true;

  public CharacterCell( boolean solid, boolean visible, Color bgColor ) {
    super( solid, visible, bgColor );
  }

  public CharacterCell( boolean solid, boolean visible, Color bgColour, String resource ) {
    super( solid, visible, bgColour, resource );
  }

  public void setFont( Font font ) {
    this.font = font;
    this.recalcBounds = true;
  }

  public void setFGColour( Color fgColour ) {
    this.fgColour = fgColour;
  }

  public void setChar( char c ) {
    this.character = c;
    this.recalcBounds = true;
  }

  public char getChar() {
    return this.character;
  }

  @Override
  public void draw( Graphics g, int x, int y, int width, int height, Color bgColour, boolean drawImage ) {
    Graphics2D g2d = (Graphics2D) g;
    super.draw( g, x, y, width, height, bgColour, drawImage );
    g2d.setFont( this.font );
    g2d.setColor( this.fgColour  );

    if ( recalcBounds ) {
      calcBounds( g2d.getFontRenderContext() );
      charX = x + (width - charBounds.getWidth()) / 2;
      charY = y + (height - charBounds.getHeight()) / 2 + charBounds.getHeight();
    }


    g2d.drawGlyphVector( this.glyph, (float) charX, (float) charY );
  }
  
  private void calcBounds( FontRenderContext frc ) {
    this.glyph = this.font.createGlyphVector(
            frc, Character.toString( this.character ) );
    this.charBounds = this.glyph.getGlyphMetrics( 0 ).getBounds2D();
    this.recalcBounds = false;
  }
}
